package com.ruoyi.web.controller.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.purchase.domain.PoPurchase;
import com.ruoyi.purchase.service.PoPurchaseService;
import com.ruoyi.storage.domain.RepoCk;
import com.ruoyi.storage.domain.RepoKc;
import com.ruoyi.storage.domain.RepoRk;
import com.ruoyi.storage.domain.RepoWare;
import com.ruoyi.storage.service.RepoCkService;
import com.ruoyi.storage.service.RepoKcService;
import com.ruoyi.storage.service.RepoRkService;
import com.ruoyi.storage.service.RepoWareService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/storage/ck")
public class StorageCkController extends BaseController {
    @Autowired
    private RepoCkService repoRkService;

    @Autowired
    private RepoWareService repoWareService;

    @Autowired
    private PoPurchaseService poPurchaseService;

    @Autowired
    private RepoKcService repoKcService;

    @RequiresPermissions("storage:ck:view")
    @RequestMapping()
    public String view() {
        return "storage/ck/storage";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(RepoCk repock) {
        startPage();
        List<RepoCk> all = repoRkService.getAll(repock);
        return getDataTable(all);
    }

    @RequestMapping("/add")
    public String add(Model model) {
        List<String> pname = new ArrayList<>();
        List<RepoCk> all = repoRkService.getAll(new RepoCk());
        for (RepoCk repoRk : all) {
            pname.add(repoRk.getMkOrderIndent());
        }
        List<PoPurchase> purId = poPurchaseService.list(new QueryWrapper<PoPurchase>().notIn("pur_id", pname).eq("rk_state",1));
        model.addAttribute("ps",purId);

        List<RepoWare> warelist = repoWareService.list();
        model.addAttribute("wares",warelist);
        model.addAttribute("user",getSysUser().getUserName());
        return "storage/ck/add";
    }

    @PostMapping("/addRe")
    @ResponseBody
    public AjaxResult addSave(@Validated RepoCk repoRk)
    {
        return toAjax(repoRkService.save(repoRk));
    }

    @RequestMapping("editStatus")
    @ResponseBody
    public AjaxResult update_status(@Validated RepoCk repoRk) {

        return toAjax(repoRkService.updateById(repoRk));
    }

    @RequestMapping("/details/{id}")
    public String details(Model model,@PathVariable String id) {
        PoPurchase poPurchase = new PoPurchase();
        poPurchase.setPurId(id);
        List<PoPurchase> purList = poPurchaseService.findPurList(poPurchase);
        RepoWare repoWare = new RepoWare();
        RepoCk repoRk1 = new RepoCk();
        repoRk1.setMkOrderIndent(id);
        List<RepoCk> repoRks = repoRkService.getAll(repoRk1);
        repoWare.setWareId(repoRks.get(0).getRepoWareId());
        List<RepoWare> repoWares = repoWareService.getList(repoWare);
        model.addAttribute("purList",purList);
        model.addAttribute("repoWare",repoWares);
        model.addAttribute("rks",repoRks);
        return "storage/ck/detail";
    }

    @RequestMapping("/detailsList/{pid}")
    @ResponseBody
    public TableDataInfo detailsList(@PathVariable String pid) {
        startPage();
        List<RepoKc> all = repoKcService.getDetailsList(pid);
        System.out.println(all);
        return getDataTable(all);
    }
}
